package com.dayatang.android.Adapter;

import java.util.HashMap;
import java.util.List;

import android.content.ContentResolver;
import android.content.ContentUris;
import android.content.ContentValues;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.provider.ContactsContract;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.QuickContactBadge;
import android.widget.TextView;
import android.widget.Toast;

import com.dayatang.android.Activity.R;

public class PersonListViewAdapter extends BaseAdapter {
	
	private Context context;
	private List<HashMap<String, Object>> listItem;
	private LayoutInflater listContainer;           

	class ListItemView{
		private Button saveBtn;
		private Button cellphoneBtn;
		private Button sellInfoBtn;
		private TextView nameTextView;
		private TextView organizationNameTextView;
		private TextView holderofanOfficeTextView;
		private TextView cellphoneTextView;
		private QuickContactBadge smallBadge;
	}
	
	public PersonListViewAdapter(Context context,List<HashMap<String, Object>> listItem) {
		this.context = context;
		this.listItem = listItem;
		listContainer = LayoutInflater.from(context);   
	}

	public int getCount() {
		return listItem.size();
	}

	public Object getItem(int position) {
		return listItem.get(position);
	}

	public long getItemId(int position) {
		return position;
	}

	public View getView(int position, View convertView, ViewGroup parent) {
		
		ListItemView  listItemView = null;
		
		if(convertView == null){
			listItemView = new ListItemView();
			convertView = listContainer.inflate(R.layout.person_list_listview_item, null);
			
			listItemView.saveBtn = (Button)convertView.findViewById(R.id.person_saveBtn);
			listItemView.cellphoneBtn = (Button)convertView.findViewById(R.id.person_cellphoneBtn);
			listItemView.sellInfoBtn = (Button)convertView.findViewById(R.id.person_sellInfoBtn);
			listItemView.nameTextView = (TextView)convertView.findViewById(R.id.person_listview_name_textView);
			listItemView.holderofanOfficeTextView = (TextView)convertView.findViewById(R.id.person_listview_holderofan_office_textView);
			listItemView.organizationNameTextView = (TextView)convertView.findViewById(R.id.person_listview_organization_name_textView);
			listItemView.cellphoneTextView = (TextView)convertView.findViewById(R.id.person_listview_cellphone_textView);
			listItemView.smallBadge = (QuickContactBadge)convertView.findViewById(R.id.badge);
			
            convertView.setTag(listItemView);    

		}else {    
            listItemView = (ListItemView)convertView.getTag();    
        }  
		
		
		final String name = (String)listItem.get(position).get("ItemName");
		final String holderofanOffice = (String)listItem.get(position).get("ItemHolderofanOffice");
		final String organizationName = (String)listItem.get(position).get("ItemOrganizationName");
		final String cellphone = (String)listItem.get(position).get("ItemCellphone");
		
		listItemView.nameTextView.setText(name);
		listItemView.holderofanOfficeTextView.setText(holderofanOffice);
		listItemView.organizationNameTextView.setText(organizationName);
		listItemView.cellphoneTextView.setText(cellphone);
		
		listItemView.smallBadge.assignContactFromPhone(cellphone, true);        
		listItemView.smallBadge.setMode(ContactsContract.QuickContact.MODE_SMALL);
		
		listItemView.saveBtn.setOnClickListener(new Button.OnClickListener() {
			public void onClick(View arg0) {
				ContentResolver resolver = context.getContentResolver();  
				Uri uri = Uri.parse("content://com.android.contacts/raw_contacts");  
				ContentValues values = new ContentValues();  
				
				long id = ContentUris.parseId(resolver.insert(uri, values));   
				
				uri = Uri.parse("content://com.android.contacts/data");  
				values.put("raw_contact_id", id);  
				values.put("data2", name);  
				values.put("mimetype", "vnd.android.cursor.item/name");  
				resolver.insert(uri, values);  
				
				values.clear(); // 清空上次的数据  
				values.put("raw_contact_id", id);  
				values.put("data1", cellphone);  
				values.put("data2", "2");  
				values.put("mimetype", "vnd.android.cursor.item/phone_v2");  
				resolver.insert(uri, values);  
		        		        
		        Toast.makeText(context, context.getString(R.string.person_save_success), Toast.LENGTH_LONG).show();
			}
		});
		
		listItemView.cellphoneBtn.setOnClickListener(new Button.OnClickListener() {
			public void onClick(View arg0) {
				Intent intent = new Intent(Intent.ACTION_CALL);   
				intent.setData(Uri.parse("tel:"+ cellphone));   
				context.startActivity(intent);  
			}
		});
		
		listItemView.sellInfoBtn.setOnClickListener(new Button.OnClickListener() {
			public void onClick(View arg0) {
				Uri smsToUri = Uri.parse("smsto:"+cellphone);
				Intent mIntent = new Intent( android.content.Intent.ACTION_SENDTO, smsToUri );
				context.startActivity( mIntent);
			}
		});
		
		return convertView;
	}

}
